package com.mengsenlin.controller;

import com.mengsenlin.entity.Result;
import com.mengsenlin.entity.User;
import com.mengsenlin.service.CartService;
import com.mengsenlin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by Administrator on 2017/7/2.
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private CartService cartService;

    /**
     * 登录
     * 访问/login,判断是否存在session，如果存在直接重定向到user.action
     * @param session
     * @param request
     * @return
     */

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(HttpSession session, HttpServletRequest request) {
        if(null != session && null != session.getAttribute("user")){
            return "redirect:/home.action";
        }
        return "/login";
    }

    @RequestMapping(value = "/isLogin", method = RequestMethod.POST)
    @ResponseBody
    public boolean isLogin(HttpSession session){
        boolean flag;           //是否登录标志，true登录、false未登录
        if(null != session && null != session.getAttribute("user")){
            flag = true;
        }else {
            flag = false;
        }
        return flag;
    }
    /**
     * 点击登录按钮后请求/signin
     * @param userName
     * @param password
     * @param session
     * @return
     */
    @RequestMapping(value = "/signin", method = RequestMethod.POST)
    @ResponseBody
    public Result<User> signIn(@RequestParam(value = "userName") String userName,
                               @RequestParam(value = "password") String password,
                               HttpServletRequest request,
                               HttpSession session) {
        return userService.signIn(userName, password, session, request);
    }




    @RequestMapping(value = "/register",method = RequestMethod.GET)
    public String register(){
        return "/register";
    }

    /**
     * 注册
     * @param userName 登录名
     * @param password 密码
     * @param phoneNum 手机号码
     * @return
     */
    @RequestMapping(value = "/signup",method = RequestMethod.POST)
    @ResponseBody
    public Result<User> signUp(@RequestParam String userName,  @RequestParam String password, @RequestParam String phoneNum){
        return userService.signUp(userName, password, phoneNum);
    }

    @RequestMapping(value = "/change",method = RequestMethod.GET)
    public String change(){ return "change";}

    /**
     * 修改
     * @param userName 用户名
     * @param password 密码
     * @param phoneNum 手机号码
     * @param session
     * @return
     */
    @RequestMapping(value = "/modify",method = RequestMethod.POST)
    @ResponseBody
    public Result<User> modify(@RequestParam String userName, @RequestParam String password, @RequestParam String phoneNum,
                               HttpSession session){
        return userService.modify(userName,password,phoneNum,session);
    }

    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public String delete(){ return "delete";}

    /**
     * 删除
     * @param userName 用户名
     * @return
     */
    @RequestMapping(value = "/cutOut",method = RequestMethod.POST)
    @ResponseBody
    public Result<User> cutOut(String userName){
        return userService.cutOut(userName);
    }

    @RequestMapping(value = "/logOut",method = RequestMethod.POST)
    @ResponseBody
    public Result<User> logOut(HttpSession session){
        return userService.logOut(session);
    }

    @RequestMapping(value = "/home/getUser.action" ,method = RequestMethod.POST)
    @ResponseBody
    public User getUser(HttpSession session){
        return (User)session.getAttribute("user");
    }

}
